<?php

namespace App\Http\Controllers\Api;

use App\EcmsHtfComment;
use App\Http\Extend\Helper;
use App\Question;
use App\UserAnswer;
use App\UserWechat;
use Curl\Curl;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redis;

class QuestionController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function index(Request $request)
    {
        if($request->get('nickName') && ($request->get('recommit') != 'true'))
        {
            $hasRecord = UserWechat::with('answer')->where('nickName',$request->get('nickName'))->first();
            $question = Question::where('type',5)->first();
            if(!empty($hasRecord->answer))
            {
                $answer = UserAnswer::where('uid',$hasRecord->id)->where('question_id',$question->id)->first();
                $hasRecord = $hasRecord->toArray();
                $timeStr = Helper::transDateTime($hasRecord['answer'][0]['created_at']);
                return response()->json([
                    'hasRecord' => true, //是否已经回答过问题
                    'timeStr' => $timeStr,
                    'pic_url' => asset('wechat/wechat'.$hasRecord['id'].'.png'),
                    'report' => Helper::report($answer->option)
                ]);
            }
        }
        $data = Question::with('option')->get()->toArray();
        return response()->json($data);
    }
    //保存一下用户提交的信息
    public function store(Request $request)
    {
        //添加用户表
        $user = UserWechat::where('nickName',$request->get('nickName'))->first();
        if(empty($user))
        {
            $user = UserWechat::create(['nickName' => $request->get('nickName')]);
        }
        else
        {
            UserAnswer::where('uid',$user->id)->delete(); //删除以前的用户选项
        }

        if($request->get('question'))
        {
            $data = [];
            $report = '';
            $question = Question::where('type',5)->first();
            foreach ($request->get('question') as $k => $v)
            {
                $qId = substr($k,8); //获取问题id
                if(is_array($v))
                {
                    foreach ($v as $k2 => $v2)
                    {
                        $data[] = [
                            'uid' => $user->id,
                            'question_id' => $qId,
                            'option' => $v2,
                            'created_at' => date('Y-m-d H:i:s')
                        ];
                    }
                }
                else
                {
                    $data[] = [
                        'uid' => $user->id,
                        'question_id' => $qId,
                        'option' => $v,
                        'created_at' => date('Y-m-d H:i:s')
                    ];
                }
                if($k == 'question'.$question->id)
                {
                    $report = $v; //头发状态的选择
                }
            }
            UserAnswer::insert($data);
        }
        $picUrl = Helper::getChartPic($user->id,$report); //产生图片

        return response()->json([
            'pic_url' => $picUrl,
            'report' => Helper::report($report)
        ]);
    }

    public function code()
    {
        $curl = new Curl();
        $curl->get('https://api.weixin.qq.com/sns/jscode2session?appid=wx79f9b85f76c5f5a9&secret=85d59a1ef0a30fef7e33c3fcbb441b55&js_code=003VRBxZ03Mfe12MkMxZ0zNDxZ0VRBx8&grant_type=authorization_code');
        if ($curl->error) {
            echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
        } else {
            dd(json_decode($curl->response));
        }
    }

    public function todo()
    {
        return response()->json([
            ['id' => 1, 'title' => 'learn vue1', 'completed' => true],
            ['id' => 2, 'title' => 'learn vue2', 'completed' => false],
            ['id' => 3, 'title' => 'learn vue3', 'completed' => true],
        ]);
    }
}
